package Client;

import java.io.*;
import java.util.Scanner;
import javax.net.ssl.*;

public class SSLClient {
	public static void main(String[] args) throws InterruptedException {
		Scanner scan = new Scanner(System.in);
		// System.out.println("Password to KeyStore: ");
		// String pass = scan.next();

		String pass = "ste11ar";
		System.setProperty("javax.net.ssl.keyStore",
				"/Users/jonathanklingberg/Downloads/Certifikat/ClientKeyStore.jks");
		System.setProperty("javax.net.ssl.trustStore",
				"/Users/jonathanklingberg/Downloads/Certifikat/ClientTrustStore.ts");
		System.setProperty("javax.net.ssl.keyStorePassword", pass);
		System.setProperty("javax.net.ssl.trustStorePassword", pass);

		String serverName = "SSLServer 3000";
		int port = 3000;
		System.out.println("Connecting to " + serverName + " on port " + port);
		boolean run = true;
		while (run) {
			try {

				SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory
						.getDefault();
				SSLSocket client = (SSLSocket) factory.createSocket(
						"172.20.10.2", port);
				System.out.println("Just connected to "
						+ client.getRemoteSocketAddress());
				OutputStream outToServer = client.getOutputStream();
				DataOutputStream out = new DataOutputStream(outToServer);

				InputStream inFromServer = client.getInputStream();
				DataInputStream in = new DataInputStream(inFromServer);

				boolean loggedIn = false;
				String output;
				String input;

				long time;
				/** Logging in **/

				while (!loggedIn) {
					System.out.print("Username: ");
					output = scan.next();
					out.writeUTF(output);

					System.out.print("Password: ");
					output = scan.next();
					out.writeUTF(output);
					input = in.readUTF();

					if (input.equals("correct")) {
						System.out.println("Welcome! \n");
						loggedIn = true;
					} else if (input.equals("timeout")) {
						System.out
								.println("You have entered wrong password 5 times, system in time-out for 30 sec!");
						time = System.currentTimeMillis();
						while ((System.currentTimeMillis() - time) < 5000) {

						}

					} else {
						System.out.println("Wrong password or username!");

					}
				}
				boolean running = true;
				while (running) {

					System.out.println("Select a command by number:");
					System.out.println("1 to get a Journal(s)");
					System.out.println("2 to modify a Journal");
					System.out.println("3 to insert a Journal");
					System.out.println("4 to delete a Journal");
					System.out.println("0 to logout");

					output = scan.next();

					/** GetJournal */
					if (output.equals("1")) {
						try {
							out.writeUTF("1");

							System.out.println("Enter a patientId: ");
							output = scan.next();
							out.writeUTF(output);
							System.out.println("Waiting for server...");
							input = in.readUTF();
							if (input.equals("fel")) {
								System.out
										.println("You have entered a non-existing patientId or you don't have permissions to fetch that journal!");
							} else {
								System.out.println(input);
								System.out.println("Done!");
							}
						} catch (Exception e) {
							System.out
									.println("You have entered a non-existing patientId or you don't have permissions to fetch that journal!");
						}
					}
					/** ModifyJournal */
					else if (output.equals("2")) {
						try {
							out.writeUTF("2");

							System.out.println("Enter a journal-nbr: ");
							output = scan.next();
							out.writeUTF(output);
							System.out.println("Enter a new text: ");
							output = scan.next();
							out.writeUTF(output);
							System.out.println("Waiting for server...");
							input = in.readUTF();
							if (input.equals("modified")) {
								System.out
										.println("Your changes have been made successfully");
							} else {
								System.out
										.println("You have entered a non-existing journal-nbr or you don't have permissions to modify that journal!");
							}
						} catch (Exception e) {
							System.out
									.println("You have entered a non-existing journal-nbr or you don't have permissions to modify that journal!");
						}
						/** InsertJournal */
					} else if (output.equals("3")) {
						try {
							out.writeUTF("3");

							System.out.println("Enter a patientId: ");
							output = scan.next();
							out.writeUTF(output);
							System.out.println("Enter an existing nurse: ");
							output = scan.next();
							out.writeUTF(output);
							System.out.println("Enter an existing doctor: ");
							output = scan.next();
							out.writeUTF(output);
							System.out.println("Enter a division: ");
							output = scan.next();
							out.writeUTF(output);
							System.out.println("Enter text for journal: ");
							output = scan.next();
							out.writeUTF(output);
							System.out.println("Waiting for server...");
							if (in.readUTF().equals("createdJournal")) {
								System.out
										.println("Your journal have been made successfully");
							} else {
								System.out
										.println("You have entered a non-existsing parameter or you don't have permissions to insert a new journal!");
							}
						} catch (Exception e) {
							System.out
									.println("You have entered a non-existing parameter or you don't have permissions to create a journal!");
						}
						/** DeleteJournal */
					} else if (output.equals("4")) {
						try {
							out.writeUTF("4");

							System.out.println("Enter a journal-nbr: ");
							output = scan.next();
							out.writeUTF(output);
							System.out.println("Waiting for server...");
							if (in.readUTF().equals("deleted")) {
								System.out
										.println("Your journal have been deleted successfully");
							} else {
								System.out
										.println("You have entered a non-existing journal-nbr or you don't have permissions to detete that journal!");
							}
						} catch (Exception e) {
							System.out
									.println("You have entered a non-existing journal-nbr or you don't have permissions to delete that journal!");
						}
						running = true;
						/** Socket close */
					} else if (output.equals("0")) {
						out.writeUTF("0");
						if (in.readUTF().equals("quit")) {
							System.out.println();
							run = true;
							running = false;
						}
					}
				}
			} catch (IOException e) {
				System.out.print("Couldn't connect to server!");
			}
			System.out.println();
			run = true;
		}

	}
}
